

/**
 * 当前类方法在线程中回调
 */
public class StnLogicCallBack:NSObject,NetworkDelegate {
    public func addPushObserver(_ observer: PushNotifyDelegate!, withCmdId cmdId: Int32) {
        
    }
    
    public func addObserver(_ observer: UINotifyDelegate!, forKey key: String!) {
        
    }
    
    public func addCGITasks(_ cgiTask: CGITask!, forKey key: String!) {
        
    }
    
    public func request2Buffer(withTaskID tid: UInt32, task: CGITask!) -> Data! {
        
        return (onSendHandler?.onPackageSendMsg(taskID: Int(tid), userContext: task))
        
    }
    
    public func buffer2Response(withTaskID tid: UInt32, responseData data: Data!, task: CGITask!) -> Int {
        return 0
    }
    
    /**
     * SDK要求上层生成长链接数据校验包,在长链接连接上之后使用,用于验证SVR身份
     *
     * @param identifyReqBuf 校验包数据内容
     * @param hashCodeBuffer 校验包的HASH
     * @param reqRespCmdID   数据校验的CMD ID
     * @return ECHECK_NOW(需要校验), ECHECK_NEVER(不校验), ECHECK_NEXT(下一次再询问)
     */
    //    @Override
    //ByteArrayOutputStream
    public func getLongLinkIdentifyCheckBuffer() ->Data{
        //ket(initConfigInfo, identifyReqBuf, hashCodeBuffer, reqRespCmdID);
        return (onLoginHandler?.onLoginSocket(initConfigInfo: initConfigInfo!))! as Data
    }
    
    /**
     * SDK要求上层解连接校验回包:登录响应
     *
     * @param buffer         SVR回复的连接校验包
     * @param hashCodeBuffer CLIENT请求的连接校验包的HASH值
     * @return 成功返回true, 失败返回false
     */
    //    @Override
    public func onLongLinkIdentifyResp(_ buffer: Data!, hashCodeBuffer: Data!) -> Bool{
        
        
        return onLoginHandler!.onLoginResp(initConfigInfo: initConfigInfo!, buffer: buffer, hashCodeBuffer: hashCodeBuffer,msgCallback: msgCallback! as AnyObject);

    }
    
    private let TAG = String(describing:StnLogicCallBack.self)
    /**
     * 处理server发送的消息
     */
    private var onPushHandler:PushHandler?;
    /**
     * 处理scoket登录逻辑
     */
    private var onLoginHandler:LoginHandler?;
    /**
     * 处理发送消息逻辑
     */
    private var onSendHandler:SendHandler?;
    /**
     * 初始化mars参数
     */
    private var initConfigInfo:InitConfigParams?;
    /**
     * 消息回调
     */
    private var msgCallback:IMsgCallBack?;

    public override init() {
        onPushHandler =  PushHandler()
        onLoginHandler = LoginHandler();
        onSendHandler =  SendHandler();
    }

    public func setMsgCallback( msgCallback:AnyObject?) {
        self.msgCallback = msgCallback as? IMsgCallBack;
    }

    public func setInitConfigInfo( initConfigInfo:InitConfigParams?) {
        self.initConfigInfo = initConfigInfo;
    }

    /**
     * SDK要求上层做认证操作(可能新发起一个AUTH CGI)
     * 如果设置了need_authed = true 会回调 makesureAuth 询问是否已经是登陆态，不是的话需要上层异步去登陆，底层任务会等待
     *
     * @return
     */
    
    public func isAuthed()->Bool  {
        return true;
    }

    /**
     * SDK要求上层做域名解析.上层可以实现传统DNS解析,或者自己实现的域名/IP映射
     *
     * @param host
     * @return
     */
    
    public func onNewDns(host:String)->[String]?  {
        IMLog.d(tag:TAG,msg:  "onNewDns host:" + host);
        return nil;
    }

    /**
     * 收到SVR PUSH下来的消息
     * 服务端响应app的消息或主动发送的消息都通过该方法回调
     *
     * @param cmdid
     * @param data
     */
    public func onPush(withCmd cid: Int, data: Data!) {
  
        onPushHandler?.onPush(cmdid: cid, data: data, imCallback: msgCallback! as AnyObject);
    }

    /**
     * SDK要求上层对TASK组包
     * client发送数据的回调
     *
     * @param taskID        任务标识
     * @param userContext
     * @param reqBuffer     组包的BUFFER
     * @param errCode       组包的错误码
     * @param channelSelect
     * @return
     */
    //ByteArrayOutputStream
//    public func req2Buf(taskID:Int,userContext:AnyObject,  reqBuffer:[UInt8], errCode:Int ,  channelSelect:Int) ->Bool {
//        //(taskID:taskID, userContext, reqBuffer, errCode, channelSelect);
//        return (onSendHandler?.onPackageSendMsg(taskID: taskID, userContext: userContext, reqBuffer: reqBuffer, errCode: errCode, channelSelect: channelSelect))!
//    }

    /**
     * SDK要求上层对TASK解包
     * client收到响应数据的回调
     *
     * @param taskID        任务标识
     * @param userContext
     * @param respBuffer    要解包的BUFFER
     * @param errCode       解包的错误码
     * @param channelSelect
     * @return int
     * 修改了通信协议,该方法不会回调:服务器发送的消息都会通过onPush回调
     */

    public func buf2Resp(taskID:Int, userContext:AnyObject, respBuffer:[Int], errCode:[Int], channelSelect:Int)->Int {
        IMLog.d(tag:TAG,msg:  "buf2Resp taskID:\(taskID)");
        return 0//RESP_FAIL_HANDLE_NORMAL;
    }

    /**
     * 任务结束回调
     * client发送任务结束的回调
     * <p>
     * 设置了task.onlySend=true 消息一发送就会回调该方法,不会等到服务器响应后回调
     *
     * @param taskID      任务标识
     * @param userContext
     * @param errType     错误类型  buf2Resp 的返回值
     * @param errCode     错误码  值和 buf2Resp 的 errCode 相同
     * @return 返回该 Task 的错误码，用作统计上报。
     */
//    @Override
    public func onTaskEnd(withTaskID tid: UInt32, task: CGITask!, errType errtype: UInt32, errCode errcode: UInt32) -> Int {
        
        IMLog.d(tag:TAG,msg:  "onTaskEnd taskID:\(tid)   errType: \(errtype)   errCode: \(errcode)");
        return Int(errcode);
    }

    /**
     * 流量统计
     *
     * @param send
     * @param recv
     */
//    @Override
    public func trafficData( send:Int, int recv:Int) {
        IMLog.d(tag:TAG,msg:"trafficData send:\(send)   recv:\(recv)");
    }

    /**
     * 连接状态通知
     *
     * @param status         综合状态，即长连+短连的状态
     *                       kNetworkUnkown = -1, kNetworkUnavailable = 0, kGateWayFailed = 1, kServerFailed = 2, kConnecting = 3, kConnected = 4, kServerDown = 5
     * @param longlinkstatus 长连状态
     *                       kConnectIdle = 0, kConnecting = 1, kConnected, kDisConnected = 3, kConnectFailed = 4
     */
//    @Override
    public func reportConnectInfo( status:Int, longlinkstatus:Int) {
        IMLog.d(tag:TAG, msg:"reportConnectInfo status:\(status)   longlinkstatus:\(longlinkstatus)");
        
        
        
    }


    /**
     * 请求做sync
     */
//    @Override
    public func requestDoSync() {
        IMLog.d(tag:TAG,msg: "requestDoSync");
    }

//    @Override
    public func requestNetCheckShortLinkHosts() ->[String] {
        IMLog.d(tag:TAG, msg: "requestNetCheckShortLinkHosts");
        return ["0"];
    }

    /**
     * 是否登录
     *
     * @return true 登录 false 未登录
     */
//    @Override
    public func isLogoned() ->Bool {
        IMLog.d(tag:TAG,msg: "iLogoned");
        return true;
    }

    /**
     * Task运行完成时，STN将Task的运行时状态及统计数据返回给上层
     *
     * @param taskString
     */
    public func reportTaskProfile( taskString:String) {
        IMLog.d(tag:TAG,msg: "reportTaskProfile:\(taskString)");
    }


}
